//
//  MYShopListVC.m
//  MuYing
//
//  Created by tbb on 2019/7/25.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYShopListVC.h"
#import <JXCategoryView/JXCategoryView.h>
#import "MYShopListClassSelectCell.h"
#import "MYShopListClassGoodsCell.h"

@interface MYShopListVC ()

@property (strong, nonatomic) NSMutableArray *goodTitles;

@property (strong, nonatomic) YQPShopSCatalogModel *model;

@end

@implementation MYShopListVC

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.goodTitles = NSMutableArray.new;
    
    [self getSCatalogListApi];
}

#pragma mark - 根据一级商品分类查询二级商品列表
- (void)getSCatalogListApi
{
    [self.goodTitles removeAllObjects];
    YQSHOWLOADINGINVIEW
    [YQPNetWorkingManager sendGetRequest:kGET_SKU_HOME_PAGE_URL(self.catalog1Id) parameters:@{} success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        if (responseObject.code == 0) {
            self.model = [YQPShopSCatalogModel mj_objectWithKeyValues:responseObject.data];
            [self.listTableView reloadData];
        }else{
            kMBMesHUD(responseObject.msg)
        }
        YQHIDELOADINGTIPS
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
    }];
}

- (void)setupView {
    [super setupView];
    self.listTableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-Height_NavBar-Height_TabBar-44);
    self.listTableView.backgroundColor = HexColor(0xF6F6F6);
    
    kYQREGISTERTABLEVIEWNIBCELL(@"MYShopListShowImageCell");
    kYQREGISTERTABLEVIEWCELL([MYShopListClassSelectCell class])
    kYQREGISTERTABLEVIEWCELL([MYShopListClassGoodsCell class])
}
#pragma mark - JXCategoryListContentViewDelegate

- (UIView *)listView {
    return self.view;
}

- (UIScrollView *)listScrollView {
    return self.listTableView;
}
#pragma mark - JXCategoryListContentViewDelegate end

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 2) {
        return self.model.recommendSkuList.count;
    }
    return 1;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        MYShopListShowImageCell *imageCell = [tableView dequeueReusableCellWithIdentifier:@"MYShopListShowImageCell"];
        return imageCell;
    }else if (indexPath.section == 1) {
        MYShopListClassSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYShopListClassSelectCell"];
        cell.model = self.model;
        return cell;
    }
    MYShopListClassGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYShopListClassGoodsCell"];
    cell.model = self.model.recommendSkuList[indexPath.row];

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        CGFloat s = (188 / 351.0);
        return (kScreenWidth-24) * s;
    }else if(indexPath.section == 1) {
        if (self.model.recommendCatalog2List.count == 0) {
            return 0.001;
        }else{
            return (kScreenWidth-24)*(237/351.0);
        }
    }else if(indexPath.section == 2) {
        if (self.model.recommendSkuList.count == 0) {
            return 0.001;
        }else{
            return 209;
        }
    }
    return 209;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 12;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end
@end
